﻿using StoreProject.BLL.SettingBLL;
using StoreProject.Entity.SettingEntity;
using StoreProject.Enum;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StoreProject.Forms.SettingForms
{
    public partial class WorkingProcedureRemarkSettingForm : Form
    {
        WorkingProcedureRemarkEntity entity = null;
        WorkingProcedureRemarkBLL _bll = new WorkingProcedureRemarkBLL();
        protected FormState _formState = FormState.INIT;
        public WorkingProcedureRemarkSettingForm()
        {
            InitializeComponent();
        }
        public WorkingProcedureRemarkSettingForm(FormState state)
        {
            InitializeComponent();
            _formState = state;
        }

        public WorkingProcedureRemarkSettingForm(FormState state, WorkingProcedureRemarkEntity entity)
        {
            this.entity = entity;
            _formState = state;
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!IsValid())
                return;
            OnSave();
        }
        public void OnSave()
        {
            WorkingProcedureRemarkEntity model = GetModel();
            bool res = false;
            if (_formState == FormState.ADD)
            {
                res = _bll.Insert(model);
            }
            else if (_formState == FormState.UPDATE)
            {
                res = _bll.Update(model);
            }
            if (res)
            {
                MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            else
            {
                MessageBox.Show("保存失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private WorkingProcedureRemarkEntity GetModel()
        {
            if (entity == null)
            {
                entity = new WorkingProcedureRemarkEntity();
                entity.Id = Guid.NewGuid().ToString();
            }
            entity.WorkingProcedureRemark = this.txtWorkingProcedureRemark.Text;
            return entity;
        }
        public bool IsValid()
        {
            errorProvider1.Clear();
            bool res = true;

            if (string.IsNullOrEmpty(txtWorkingProcedureRemark.Text.Trim()))
            {
                res = false;
                errorProvider1.SetError(txtWorkingProcedureRemark, "客户名称不能为空！");
            }
            return res;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void WorkingProcedureRemarkSettingForm_Load(object sender, EventArgs e)
        {
            InitData();
        }
        private void InitData()
        {
            if (_formState == FormState.ADD)
            {
                this.Text = "新增工艺备注";
            }
            else if (_formState == FormState.UPDATE)
            {
                this.Text = "修改工艺备注";
            }
            if (this.entity != null)
            {
                this.txtWorkingProcedureRemark.Text = this.entity.WorkingProcedureRemark;
            }
        }
    }
}
